<?php
	$user = $_POST['user'];
	$invites = array();
	if($db = new SQLiteDatabase('cs147organizer.db')){
		$command = "SELECT * FROM project_invitations
		WHERE user_id=%d AND deleted=0";
		$replaced=sprintf($command,$user);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			$inv = array();
			$inv['projectid'] = $cur['project_id'];
				$uncom = "SELECT * FROM users WHERE user_id=%d LIMIT 1";
				$uncom = sprintf($uncom, $cur['inviter_id']);
				$r = $db->query($uncom);
				if($r->valid())	{
					$c = $r->current();
					$inv['inviter'] = $c['username'];
				}
				$prcom = "SELECT * FROM projects WHERE project_id=%d LIMIT 1";
				$prcom = sprintf($prcom, $cur['project_id']);
				$r = $db->query($prcom);
				if($r->valid())	{
					$c = $r->current();
					$inv['projectname'] = $c['project_name'];
				}
			array_push($invites, $inv);
			$result->next();
		}
		echo(json_encode($invites));
	} else {
		die($err);
	}
?>